package com.example.benefitcommunity.service.impl;

import com.example.benefitcommunity.entity.BcComment;
import com.example.benefitcommunity.entity.BcReply;
import com.example.benefitcommunity.dao.BcReplyDao;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcReplyService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 回复表(BcReply)表服务实现类
 *
 * @author makejava
 * @since 2025-08-30 17:41:15
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcReplyService")
public class BcReplyServiceImpl implements BcReplyService {
    @Resource
    private BcReplyDao bcReplyDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BcReply queryById(Integer id) {
        return this.bcReplyDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bcReply     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcReply> queryByPage(BcReply bcReply, PageRequest pageRequest) {
        long total = this.bcReplyDao.count(bcReply);
        return new PageImpl<>(this.bcReplyDao.queryAllByLimit(bcReply, pageRequest), pageRequest, total);
    }

    /**
     * @param commentid 筛选条件
     * @return
     */
    @Override
    public Result<Object> queryByReply(Integer commentid) {
        BcReply bcReply = new BcReply();
        bcReply.setCommentid(commentid);
        List<BcReply> list = this.bcReplyDao.queryAll(bcReply);
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) list.size(), list);
    }

    /**
     * 新增数据
     *
     * @param bcReply 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> insert(BcReply bcReply) {
        if (this.bcReplyDao.insert(bcReply) > 0) {
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "回复新增成功！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "回复新增失败！");
    }

    /**
     * 修改数据
     *
     * @param bcReply 实例对象
     * @return 实例对象
     */
    @Override
    public BcReply update(BcReply bcReply) {
        this.bcReplyDao.update(bcReply);
        return this.queryById(bcReply.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bcReplyDao.deleteById(id) > 0;
    }
}
